//
//  BiDatePickerView.m
//  XinBanEducation
//
//  Created by bindu on 14-6-4.
//  Copyright (c) 2014年 Nahan Cop. All rights reserved.
//

#import "BiDatePickerView.h"

@interface BiDatePickerView()
{
    UIDatePicker * _datePicker;
}
@end

@implementation BiDatePickerView
@synthesize delegate;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.7];
        
        _datePicker = [[UIDatePicker alloc] initWithFrame:CGRectZero];
        _datePicker.backgroundColor = [UIColor whiteColor];
        _datePicker.datePickerMode = UIDatePickerModeDateAndTime;
        [self addSubview:_datePicker];
    
        _datePicker.frame = CGRectMake(0, 0, MAIN_SCREEN_WIDTH, _datePicker.frame.size.height);
        
        UIView * bgView = [[UIView alloc] initWithFrame:CGRectMake(0, frame.size.height-_datePicker.frame.size.height-40, frame.size.width, 40)];
        bgView.backgroundColor = UIColorFromRGB(0xebebeb);
        [self addSubview:bgView];
    
      
        UIButton * cancelBtn = GET_BUTTON(CGRectMake(5, (40-30)/2, 50, 30), 12, NO, [UIColor blackColor]);
        [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        [cancelBtn addTarget:self action:@selector(onCancelBtnClick) forControlEvents:UIControlEventTouchUpInside];
        cancelBtn.tag = 0x0001;
        [bgView addSubview:cancelBtn];
        
        UIButton  * okBtn = GET_BUTTON(CGRectMake(MAIN_SCREEN_WIDTH-5-50, (40-30)/2, 50, 30), 12, NO, [UIColor blackColor]);
        [okBtn setTitle:@"确定" forState:UIControlStateNormal];
        [okBtn addTarget:self action:@selector(onOkBtnClick) forControlEvents:UIControlEventTouchUpInside];
        okBtn.tag = 0x0002;
        [bgView addSubview:okBtn];
        
        _datePicker.frame = CGRectMake(0, frame.size.height-_datePicker.frame.size.height, _datePicker.frame.size.width, _datePicker.frame.size.height);
    }
    return self;
}

- (void)onCancelBtnClick
{
    [self removeFromSuperview];
}

- (void)onOkBtnClick
{
    [self.delegate onDatePickerViewDateSelect:self date:_datePicker.date];
    [self removeFromSuperview];
}


-(void)setDateModeandButton
{
    
    UIButton *canclebtn = (UIButton*)[self viewWithTag:0x0001];
    canclebtn.hidden = YES;
    UIButton *okbtn = (UIButton*)[self viewWithTag:0x0002];
    okbtn.frame = CGRectMake(MAIN_SCREEN_WIDTH/2-50, 0, 100, 40);
//    if (isIpad) {
//          okbtn.frame = CGRectMake(MAIN_SCREEN_WIDTH/2-100, 0, 200, 80);
//    }
    okbtn.titleLabel.font = [UIFont systemFontOfSize:14.0];
}

@end
